﻿//EasyUI弹出层基类EasyLayer.js
var easyLayer = function(olayer,zindex,width,height){
	this.layer = olayer;//层元素
	this.zindex = zindex;//Z坐标
	this.width = width;//指定宽
	this.height = height;//指定高
	this.onbeforeopen = null;//事件：开启前，返回值(true|false)
	this.onopen = null;//事件：开启后
	this.onbeforeclose = null;//事件：关闭前，返回值(true|false)
	this.onclose = null;//事件：关闭后
	this.isopen = false;//开启状态
	this.easyLayerInit = function(){
		if(!this.layer){
			this.layer = document.createElement('div');
			document.body.appendChild(this.layer);
		}
		var iw = this.width,ih = this.height;
		this.layer.style.display = 'none';
		this.layer.style.position = 'absolute';
		this.layer.style.zIndex = this.zindex||'1000';
		if(iw){this.layer.style.width = (isNaN(iw)&&iw.indexOf('%')!=-1)?iw:iw+'px';}
		if(ih){this.layer.style.height = (isNaN(ih)&&ih.indexOf('%')!=-1)?ih:ih+'px';}
	};
	this.open = function(x,y){
		var bnext = true;
		if(this.onbeforeopen&&this.onbeforeopen.constructor==Function){bnext = this.onbeforeopen();}
		if(!bnext){return false;}
		var xdde = document.documentElement,xdb = document.body,dbox = this.layer;
		dbox.style.position = 'absolute';
		dbox.style.display = 'block';
		var dbow = dbox.offsetWidth||this.width,dboh = dbox.offsetHeight||this.height,rs = easyUI.getWindowSize();
		x = (!isNaN(x))?x:Math.max(xdde.scrollLeft,xdb.scrollLeft)+Math.floor((rs.width-dbow)/2);
		y = (!isNaN(y))?y:Math.max(xdde.scrollTop,xdb.scrollTop)+Math.floor((rs.height-dboh)/2);
		dbox.style.left = x+'px';
		dbox.style.top = y+'px';
		this.isopen = true;
		if(this.onopen&&this.onopen.constructor==Function){this.onopen();}
	};
	this.close = function(){
		var dbox = this.layer;
		var bnext = true;
		if(this.onbeforeclose&&this.onbeforeclose.constructor==Function){bnext = this.onbeforeclose();}
		if(!bnext){return false;}
		dbox.style.display = 'none';
		this.isopen = false;
		if(this.onclose&&this.onclose.constructor==Function){this.onclose();}
	};
	this.easyLayerInit();
};